{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3b4fa77a-bcf7-4db6-9673-4419892f72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f60c9ffa-b59b-4434-96e8-52cb77faf083",
   "metadata": {},
   "source": [
    "###使用Numpy读写数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bc6c7bbb-5cea-4f03-814e-fff2cbdec317",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing liao.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile liao.txt  ### 创建一个文本文档，名称叫 liao\n",
    "1 2 3 4 5 6\n",
    "2 3 4 5 6 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fc1608e9-4bbf-41f3-962c-a81a7219b112",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [2., 3., 4., 5., 6., 7.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = []\n",
    "with open('liao.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        fileds = line.split()\n",
    "        cur_data = [float(x) for x in fileds]\n",
    "        data.append(cur_data)\n",
    "data = np.array(data)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3de66b6f-c89b-4ef9-b0ce-17c31be5cf7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [2., 3., 4., 5., 6., 7.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('liao.txt') ### numpy库的函数 loadtxt\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "32fafd9a-2188-47a7-81fa-2ef5140de86e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing liao2.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile liao2.txt\n",
    "1,2,3,4,5,6\n",
    "2,3,4,5,6,7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "79f7be1d-c5db-427b-9e8c-2b133e104ef6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [2., 3., 4., 5., 6., 7.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('liao2.txt',delimiter = ',')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "58a3c502-4ee3-4977-9e7f-74f1bd96a07c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing liao3.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile liao3.txt\n",
    "x,y,z,w,a,b\n",
    "1,2,3,4,5,6\n",
    "2,3,4,5,6,7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e401fdda-6c63-4c00-9531-27b326cd2be1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3., 4., 5., 6.],\n",
       "       [2., 3., 4., 5., 6., 7.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('liao3.txt',delimiter = ',',skiprows = 1)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c5c1940-f21c-4f1b-bb6d-e4d9600e531c",
   "metadata": {},
   "source": [
    "· 'liao3.txt' 路径和代码最好放在一起\n",
    "· skiprows  去掉几行\n",
    "· delimtert = ','  分隔符为 ,\n",
    "· usecols = (0,1,4)  指定使用哪几列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "53f408f0-9ba5-469a-bb54-9485810cefcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "liao_array = np.array([[1,2,3],[4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "14aa9b45-006f-4a26-80d1-a7af60f979c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('liao4.txt',liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0eb66644-9512-43e7-989d-47cedf7a43ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('liao4.txt',liao_array,fmt='%d',delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ce011e9a-cc51-4e3e-a61f-3f6ec18e8413",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('liao4.txt',liao_array,fmt='%.2f',delimiter=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7e0ac42-3a19-4b5a-8605-780f6e31bf0f",
   "metadata": {},
   "source": [
    "### 读写array结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "aa49af75-89c4-48f9-9382-71b964428542",
   "metadata": {},
   "outputs": [],
   "source": [
    "liao_array = np.array([[1,2,3],[4,5,6]])\n",
    "np.save('liao_array.npy',liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "6b2c089f-e2db-4da6-b71a-3ef2e32fd393",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao = np.load('liao_array.npy')\n",
    "liao"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "fbc16064-3dd2-4416-afbd-56675fab18f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array2 = np.arange(10)\n",
    "liao_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "7e12ca41-bab4-4efa-a2f5-dff4d3e2f100",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.savez('liao.npz',a=liao_array,b=liao_array2)\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e0fab52a-1ff5-401d-83d0-83d278d4ced0",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.load('liao.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "87019591-107e-4bbb-804d-8d37e2b73f1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KeysView(NpzFile 'liao.npz' with keys: a, b)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "4430272f-0908-4cd2-91f4-04bacee6cc57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['a']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "a99f5226-ce15-4e73-984c-cdbb15c0777b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb9df6b0-e7eb-495e-a8da-30e592cbd84a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
